import {createRouter, createWebHistory} from "vue-router";
import Login from "../page/login.vue";
import BasicLayout from "../layout/BasicLayout.vue";
import {ElMessage} from "element-plus";
import DashBoard from "../page/DashBoard.vue";
import User from "../page/User.vue";
import Group from "../page/Group.vue";
import Blog from "../page/Blog.vue";
import Review from "../page/Review.vue";

const routes = [
    {path: '/', redirect: '/login'},
    {path: '/login', component: Login},
    {path: '/home', component: BasicLayout,
        children :[
            {path: '/dashboard', component: DashBoard},
            {path: '/user', component: User},
            {path: '/group', component: Group},
            {path: '/blog', component: Blog},
            {path: '/review', component: Review},
        ]
    },
]

const router = createRouter({
    history: createWebHistory(),
    routes
})
router.beforeEach((to,from,next)=>{
    if (to.path==='/login') return next()
    let token = sessionStorage.getItem('token');
    if (!token){
        ElMessage.error('登录状态,请重新登录')
        return next('/login')
    }
    next()
})

export default router